<?php
/**
 * Class descript.
 * User: chan
 * Date: 2017/10/26 15:29
 */
namespace app\api\controller;
use think\Db;
use app\api\model\Message;

class UserMessage extends Base
{
    /**
     * 消息记录
     * 
     */
    public function messageList(){
        $input= input();
        if (empty($input['uid'])){
              return $this->ajaxReturn('',-1,'未定义uid');
        }
        $recommend_id = empty($input['recommend_id']) ? '' : $input['recommend_id'];
        $data = [];
        $recommend = Db::table('ty_team')
        ->alias('t')
        ->join('ty_user_account u','t.uid = u.id')
        ->where('t.uid',$recommend_id)
        ->field('t.uid,t.recommend_id,t.level,u.head_url,u.nick_name,u.real_name')
        ->find();
        if ($recommend) {
            $recommend_sum = db('message')->where(['uid'=>$recommend['uid'],'tid'=>$input['uid'],'tid_status'=>0])->count();
            $recommend_t['sum'] = $recommend_sum;
            $data[] = $recommend;//推荐人
        }

        $recommend_t = Db::table('ty_team')
        ->alias('t')
        ->join('ty_user_account u','t.uid = u.id')
        ->where('t.uid',$recommend['recommend_id'])
        ->field('t.uid,t.recommend_id,t.level,u.head_url,u.nick_name,u.real_name')
        ->find();
        if ($recommend_t) {
            $recommend_t_sum = db('message')->where(['uid'=>$recommend_t['uid'],'tid'=>$input['uid'],'tid_status'=>0])->count();
            $recommend_t['sum'] = $recommend_t_sum;
            $data[] = $recommend_t;//推荐人的上级
        }
        $team1 = Db::table('ty_team')//一级团队
        ->alias('t')
        ->join('ty_user_account u','t.uid = u.id')
        ->where('t.recommend_id',$input['uid'])
        ->field('t.uid,t.recommend_id,t.level,u.head_url,u.nick_name,u.real_name')
        ->select();
        foreach ($team1 as $key => $value) {
            $value['sum'] = db('message')->where(['uid'=>$value['uid'],'tid'=>$input['uid'],'tid_status'=>0])->count();
            $data[] = $value;
            $team2 = Db::table('ty_team')//一级团队
            ->alias('t')
            ->join('ty_user_account u','t.uid = u.id')
            ->where('t.recommend_id',$value['uid'])
            ->field('t.uid,t.recommend_id,t.level,u.head_url,u.nick_name,u.real_name')
            ->select();
            foreach ($team2 as $k => $v) {
                $v['sum'] = db('message')->where(['uid'=>$v['uid'],'tid'=>$input['uid'],'tid_status'=>0])->count();
                $data[] = $v;
            }
        }
      return $this->ajaxReturn($data);
    }

}